package com.chenyaowei.happyhub.controller;

import com.chenyaowei.happyhub.mapper.GoodsMapper.GoodIntroMapper;
import com.chenyaowei.happyhub.mapper.GoodsMapper.GoodParameterMapper;
import com.chenyaowei.happyhub.mapper.GoodsMapper.GoodSlideMapper;
import com.chenyaowei.happyhub.mapper.GoodsMapper.GoodVersionMapper;
import com.chenyaowei.happyhub.service.GoodsService.GoodService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.chenyaowei.happyhub.mapper.GoodsMapper.*;
import com.chenyaowei.happyhub.mapper.IndexNavMapper;
import com.chenyaowei.happyhub.pojo.Advertisement.IndexAd;
import com.chenyaowei.happyhub.pojo.GoodsPojo.Good;
import com.chenyaowei.happyhub.pojo.GoodsPojo.GoodDetail;
import com.chenyaowei.happyhub.pojo.IndexNav;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/")
public class WeChatController {
    @Resource
    GoodService goodService;
    @Resource
    IndexNavMapper indexNavMapper;
    @Resource
    GoodVersionMapper goodVersionMapper;
    @Resource
    GoodParameterMapper goodParameterMapper;
    @Resource
    GoodSlideMapper goodSlideMapper;
    @Resource
    GoodIntroMapper goodIntroMapper;
    static int indexTotalPages=2;
    static int indexPageNum=1;
    String str;
    int pageSize=20;
    ObjectMapper mapper=new ObjectMapper();
    PageInfo<Good> pageInfo;

    @RequestMapping("/wechat/selectgooddetail")
    String goodList(int id)throws Exception{
        GoodDetail goodDetail=new GoodDetail(
                goodService.selectGoodById(id),
                goodVersionMapper.selectGoodVersionById(id),
                goodParameterMapper.selectGoodParameterById(id),
                goodIntroMapper.selectGoodIntroById(id),
                goodSlideMapper.selectGoodSlideById(id));
        str=mapper.writeValueAsString(goodDetail);
        return str;
    }
    @RequestMapping("/wechat/selectindexgoods")
    String goodsList(int pageNum,String keyword)throws Exception{
        Map<String,Object> map=new HashMap<>();
        List<Good> goods;
        IndexAd indexAd=new IndexAd();
        List<IndexAd> indexAdList=new ArrayList<>();
        pageInfo = goodService.selectGoods(pageNum,pageSize);
        pageInfo.getList();
        goods = pageInfo.getList();
        map.put("totalPages",pageInfo.getPages());
        indexAd.setGoodList(goods);
        if(indexPageNum>indexTotalPages)
            indexPageNum=1;
        pageInfo=goodService.selectGoodsByAd(indexPageNum,1);
        indexPageNum+=1;
        indexTotalPages=pageInfo.getPages();
        goods = pageInfo.getList();
        indexAd.setAdGood(goods.get(0));
        indexAdList.add(indexAd);
        map.put("section",indexAdList);
        str=mapper.writeValueAsString(map);
        System.out.println("keyword is:"+keyword+"   and pageNum is:"+pageNum);
        System.out.println("indexPageNum: "+indexPageNum);
        System.out.println("indexTotalPages: "+indexTotalPages);
        System.out.println(str);
        return str;
    }
    @RequestMapping("/wechat/selectgoods")
    String goodList(int pageNum,String keyword)throws Exception{
        List<Good> goods;
        Map<String,Object> map=new HashMap<>();
        if (keyword==null) {
            pageInfo = goodService.selectGoods(pageNum,pageSize);

        }
        else {
            pageInfo=goodService.selectGoodsByKeyword(pageNum,pageSize,keyword);
        }
        goods = pageInfo.getList();
        map.put("totalPages",pageInfo.getPages());
        map.put("goods",goods);
        str=mapper.writeValueAsString(map);
        System.out.println("keyword is:"+keyword+"   and pageNum is:"+pageNum);
        System.out.println(str);
        return str;
    }
    @RequestMapping("/wechat/selectgoodsbytype")
    String goodListByType(int pageNum,String keyword)throws Exception{
        List<Good> goods;
        Map<String,Object> map=new HashMap<>();
        pageInfo=goodService.selectGoodsByType(pageNum,pageSize,keyword);
        goods = pageInfo.getList();
        map.put("totalPages",pageInfo.getPages());
        map.put("goods",goods);
        str=mapper.writeValueAsString(map);
        System.out.println("keyword is:"+keyword+"   and pageNum is:"+pageNum);
        System.out.println(str);
        return str;
    }

    @RequestMapping("/wechat/selectindexnav")
    String selectIndexNav()throws Exception{
        ObjectMapper mapper=new ObjectMapper();
        List<IndexNav> indexNavs=indexNavMapper.selectAllIndexNav();
        String str=mapper.writeValueAsString(indexNavs);
        return str;
    }
    @RequestMapping("/wechat/selectAllType")
    String selectAllType()throws Exception{
        ObjectMapper mapper=new ObjectMapper();
        List<IndexNav> indexNavs=indexNavMapper.selectAllType();
        String str=mapper.writeValueAsString(indexNavs);
        return str;
    }
}
